<?php

require_once( DIR_INCLUDE .'/table.class.php');
require_once( 'media.class.php');


if($_SERVER['REQUEST_METHOD'] == 'POST'){

  if($_POST['do']=='delete'){
    $deleted_count =0;
    foreach($_POST['media_ids'] as $e_media_id){
      $e_media = new media($e_media_id);
      if($e_media->delete()){
        $deleted_count++;  
      }
    }
    
    set_message( $deleted_count . ' Media Deleted');
    reload_page();
    exit;  
  }
  
}

$fields = array('id'=>'ID',
                'filename'=>'Title / Filename',
                'filetype'=>'Type',
                'in_page'=>'In Page',
                'filesize'=>'Size',
                'last_modified_date'=>'Date'
                );
                
$sort = $_GET['sort'];
if($sort=='')$sort = 3;                
$sort_keys = array_keys($fields);
$order_by = $sort_keys[$sort];
$sort_ad = ($_GET['ad']== 'a'?'asc':'desc');

// Fill mediafiles object
if($media_dir = opendir(media::get_settings()->directory))
{
	while(($media_file = readdir($media_dir)) !== false)
	{
		if($media_file == '..' or $media_file == '.') continue;
		
		try
		{
			$media_file = new media($media_file);
			$media_files[] = $media_file;
		}
		catch(exception $ex) {}				
	}
	if($order_by != 'id' and $order_by != 'in_page')
		usort($media_files, array("media", "sort_by_" . $order_by . "_" . $sort_ad));
}


/* Modify Query for paging */
$max_item = get_setting('LIST_MAX_ITEM'); // Maximum number of item listed in one page
$total_item = count($media_files);

$p = $_GET['p'];
process_paging($max_item , $total_item, $p, $total_page, $offset );
// $query .= " ORDER BY sort, project_id LIMIT " . $max_item . " OFFSET " . $offset ;
//$query .= " ORDER BY ". parse_order_by($order_by, $sort_ad) ." LIMIT " . $max_item . " OFFSET " . $offset ; 

$rsconn = db_query($query,true);

$layout->add('toolbar','<a href="'. WWW_ROOT .'admin/modules/media_files/edit_media" class="button">Add Media</a>');

show_session_message();

$content.='<h2>Media</h2>';

$content.='<form id="form_media" action="" method="POST">';
$content.='<input type="hidden" name="do" id="do" value="" />';
$content.='<div class="toolbar"><a href="#" onclick="if(confirm(\'Are you sure want to delete selected media?\')){ $(\'#do\').val(\'delete\'); $(\'#form_media\').submit();} return false;" class="button">Delete Selected</a></div>';
//if($rsconn){
	
	
if(is_array($media_files))
{		
	for($i = $offset; $i < ($offset + $max_item); $i++)
	{		
		if($i >= count($media_files)) break;
		
		$dat[] = array( 
			'<input type="checkbox" class="check_item" name="media_ids[]" value="'.$media_files[$i]->filename.'"> ',
			'<a href="'. WWW_ROOT .'admin/modules/media_files/edit_media?id='.$media_files[$i]->filename .'">'. $media_files[$i]->filename .'</a>'
			.'<div class="small">'.$media_files[$i]->filename .'</div>',
			$media_files[$i]->filetype,
			$page_link,
			(number_format(($media_files[$i]->filesize * 0.0009765625),1) ).'Kb',
			($media_files[$i]->last_modified_date == '' ? '' : date("m/d/Y", $media_files[$i]->last_modified_date))
			);
	}
}  
//}



$head = table_list::prepare_head_array($fields);
$table_options['table_class'] = 'listview';
$table_options['table_attribute'] = 'width="100%"';
$table_options['col_width'] = array(60,'*',100,'*',140);
$table = new table_list($head, $dat, $table_options);
$table->sort_column($_GET['sort']);
$content.= $table->to_string();


$content.='</form>';



$content.= generate_paging($p, $total_page);
$layout->add('content', $content);


function replace_brackets($str){
  $str = str_replace('][',', ', $str);
  $str = str_replace(']','', $str);
  $str = str_replace('[','', $str);
  
  return $str;
    
}

function parse_order_by($order_by, $sort_ad){
  $order_bys = split(",", $order_by);
  foreach($order_bys as $e_order_by){
    $ret[] = $e_order_by . ' ' . $sort_ad;  
  }
  return implode(", ", $ret);
  
}

?>